﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace Ramparts
{
    class BuildPhase : Scene
    {

        float fTotalElapsedTime = 0;
        private const float MovementRate = 500f;

        TetrisPiece tetrisPiece;
        TetrisEngine tetrisEngine;
        WallAnimation wallAnimation;

        GraphicTimer countdownTimer;

        public BuildPhase(SceneManager sceneManager)
        {
            this.sceneManager = sceneManager;
        }

        public override bool initScene()
        {
            
            base.initScene();
            wallAnimation = new WallAnimation(5,2, sceneManager.world);

            viewportRect = new Rectangle(0, 0,
                                                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Width,
                                                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Height);
            camera.ManualCamera = false;
            camera.Position = new Vector2(-1 * viewportRect.Width / 2, -1 * viewportRect.Height / 2);
            previousMouseState = Mouse.GetState();
            tetrisEngine = new TetrisEngine();
            tetrisPiece = tetrisEngine.getNextPiece(sceneManager, new Vector2(previousMouseState.X, previousMouseState.Y));
           
             countdownTimer = new GraphicTimer(new Vector2(viewportRect.Width / 2, viewportRect.Height), sceneManager, 30, -1, 0);

             base.sceneComponents.Add(wallAnimation);
             wallAnimation.start();

             createBoats();

             return true;
        
         }


        public void createBoats()
        {

            Boat boat = new Boat(500, 500, sceneManager);
            boat.name = "Boat";

            Boat boat2 = new Boat(600, 500, sceneManager);
            boat.name = "Boat Two";

            sceneManager.world.enemyList.Add(boat);
            sceneManager.world.enemyList.Add(boat2);

        }

        public override void drawScene()
        {

            base.drawScene();
            sceneManager.world.Draw();
            drawComponents();
            
            if (!wallAnimation.isPlaying)
                tetrisPiece.Draw();
            
            sceneManager.rampart.spriteBatch.End();
            sceneManager.rampart.spriteBatch.Begin();
            countdownTimer.Draw();
            sceneManager.rampart.spriteBatch.End();
            
        }

        protected override void processMouseEvents(GameTime gameTime)
        {
            base.processMouseEvents(gameTime);
            
            Vector2 mousePos = new Vector2(currentMouseState.X, currentMouseState.Y);
            Vector2 correctedMousePos = new Vector2(mousePos.X - 40, mousePos.Y - 40);

            tetrisPiece.position = correctedMousePos;

            if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
            {
                if (viewportRect.Contains((int)mousePos.X, (int)mousePos.Y))
                {
                    Vector2 tilePos = sceneManager.world.wallLayer.onTile(correctedMousePos);
                    if (sceneManager.world.putDownPiece(tetrisPiece, correctedMousePos))
                    {
                        tetrisPiece = tetrisEngine.getNextPiece(sceneManager, correctedMousePos);
                    }
                }
            }

            if (currentMouseState.RightButton == ButtonState.Released && previousMouseState.RightButton == ButtonState.Pressed)
            {
                tetrisPiece.rotate();
            }

            previousMouseState = currentMouseState;
        }

        protected override void processKeyboardEvents(GameTime gameTime)
        {
            base.processKeyboardEvents(gameTime);
        
            if (currentKeyboardState.IsKeyDown(Keys.S))
            {
                sceneManager.world.setIso();
            }

            if (currentKeyboardState.IsKeyDown(Keys.A))
            {
                sceneManager.world.set2d();
            }

            previousKeyboardState = currentKeyboardState;
        }

        public override void Update(GameTime gameTime)
        {

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            sceneManager.world.Update(gameTime);

            base.Update(gameTime);

            if (sceneManager.world.newWorld)
            {
                if (wallAnimation.isPlaying)
                {
                    wallAnimation.update(gameTime);
                }
                else
                {
                    sceneManager.world.newWorld = false;
                    countdownTimer.startTimer();
                }
            }
            else
            {
                processKeyboardEvents(gameTime);
                processMouseEvents(gameTime);
                countdownTimer.Update(gameTime);
                sceneManager.world.updateEnemies(gameTime);
            }

            checkEndConditions();
            fTotalElapsedTime += elapsed;
        
            }



        private void checkEndConditions()
        {
            // TODO this needs to run when the timer has hit 0;
            //    if (1 == 0)
            //        {
            if (sceneManager.world.GetScore(0))
            {
                CannonPhase newScene = new CannonPhase(this.sceneManager);
                newScene.initScene();
                sceneManager.SwapScenes(newScene);

            }
            else
            {
                //        sceneManager.AddScene(new GameOver());
            }

            //       }
        }
        
        }
    }


